package cn.com.inossem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.foxykeep.datadroid.requestmanager.Request;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import cn.com.inossem.adapter.ListJobsItemAdapter;
import cn.com.inossem.base.RequestActivity;
import cn.com.inossem.model.QueryProjectInfoBean;
import cn.com.inossem.model.QueryProjectOpportunityInfoBean;
import cn.com.inossem.pulltorefresh.widget.XListView;
import cn.com.inossem.pulltorefresh.widget.XListView.IXListViewListener;
import cn.com.inossem.requestmanager.WooRequestFactory;
import cn.com.inossem.utils.ActivityStackControlUtil;
import cn.com.inossem.utils.Utils;

/**
 * 
 * 收藏申请列表
 * 
 * <p>
 * detailed comment
 * 
 * @author szc 2015-8-24
 * @see
 * @since 1.0
 */
public class ListJobsActivity extends RequestActivity implements
		IXListViewListener, OnClickListener {

	XListView mMessagesList;
	ListJobsItemAdapter adapter;
	public static List<QueryProjectInfoBean> Xuanzhongmessage;
	List<QueryProjectInfoBean> messageInfo;
	ImageView more;
	TextView wancheng;
	int type = 0;// 0收藏1申请
	QueryProjectOpportunityInfoBean bean;
	private boolean isRefresh = false;

	ImageView back;

	private Handler mHandler = new Handler(new Handler.Callback() {

		@Override
		public boolean handleMessage(Message msg) {
			adapter.notifyDataSetChanged();
			mMessagesList.stopRefresh();
			mMessagesList.stopLoadMore();
			mMessagesList.setRefreshTime(getTime());
			return false;
		}
	});

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main_tab_layout);
		type = getIntent().getIntExtra("type", 0);
		System.out.println("type:" + type);
		initView();

	}

	@Override
	public void onDestroy() {
		ActivityStackControlUtil.remove(this);
		super.onDestroy();
	}

	private void initView() {
		back = (ImageView) this.findViewById(R.id.back);
		back.setOnClickListener(this);
		TextView title = (TextView) this.findViewById(R.id.title);
		more = (ImageView) this.findViewById(R.id.more);
		more.setOnClickListener(this);
		wancheng = (TextView) this.findViewById(R.id.wancheng);
		wancheng.setOnClickListener(this);
		if (type == 0) {
			title.setText("我的收藏");
		} else if (type == 1) {
			title.setText("我的项目");
			more.setVisibility(View.GONE);
		}
		mMessagesList = (XListView) this.findViewById(R.id.listview);
		mMessagesList.setPullRefreshEnable(true);
		mMessagesList.setPullLoadEnable(true);
		mMessagesList.setXListViewListener(this);
		mMessagesList.setRefreshTime(getTime());
		messageInfo = new ArrayList<>();
		Xuanzhongmessage = new ArrayList<>();
		bean = new QueryProjectOpportunityInfoBean();
		if (type == 0) {
			bean.pageNum = 1;
			bean.type = 2;
		} else if (type == 1) {
			bean.pageNum = 1;
			bean.type = 3;
		}
		launchRequest(WooRequestFactory.getJobsOpportunityDataRequest(bean));
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	public Request getInitialRequest() {
		return null;

	}

	@Override
	public void onRequestSucess(Request request, Bundle bundle) {
		if (bundle.containsKey(WooRequestFactory.BUNDLE_JOBSOPPORTUNITY_LIST)) {
			// messageInfo = new ArrayList<QueryProjectInfoBean>();
			List<QueryProjectInfoBean> retList = bundle
					.getParcelableArrayList(WooRequestFactory.RESPONSE_JOBSOPPORTUNITY_DATA);
			if (retList == null) {
				adapter.notifyDataSetChanged();
				if (isRefresh) {
					mMessagesList.stopRefresh();
					mMessagesList.stopLoadMore();
					mMessagesList.setRefreshTime(getTime());
					Toast.makeText(this, "当前是最后一页！", Toast.LENGTH_LONG).show();
				}
				return;
			}
			if (retList.size() > 0) {
				if (retList.size() < 10) {
					mMessagesList.setPullLoadEnable(false);
				}
				if (isRefresh) {
					mMessagesList.stopRefresh();
					mMessagesList.stopLoadMore();
					mMessagesList.setRefreshTime(getTime());
				}
				messageInfo.addAll(retList);
				for (int i = 0; i < messageInfo.size(); i++) {
					messageInfo.get(i).projectInfoID = messageInfo.get(i).id;
				}

				System.out.println("长度：" + messageInfo.size());
				if (adapter == null) {
					adapter = new ListJobsItemAdapter(this, messageInfo, type,
							Xuanzhongmessage);
					mMessagesList.setAdapter(adapter);
					adapter.notifyDataSetChanged();
				} else {

					adapter.setList(messageInfo);
					adapter.notifyDataSetChanged();
				}

			} else {
				if (adapter != null)
					adapter.notifyDataSetChanged();
				if (isRefresh) {
					mMessagesList.stopRefresh();
					mMessagesList.stopLoadMore();
					mMessagesList.setRefreshTime(getTime());
				}
			}
		} else if (bundle
				.containsKey(WooRequestFactory.BUNDLE_EXTRA_APPLYQUXIAO)) {
			System.out.println("取消收藏成功！");
			for (int i = 0; i < Xuanzhongmessage.size(); i++) {
				if (messageInfo.contains(Xuanzhongmessage.get(i)))
					messageInfo.remove(Xuanzhongmessage.get(i));
			}
			Xuanzhongmessage = new ArrayList<QueryProjectInfoBean>();
			adapter.notifyDataSetChanged();
		} else if (bundle
				.containsKey(WooRequestFactory.BUNDLE_EXTRA_UPDATESUCCESS)) {

			mMessagesList.setPullLoadEnable(true);
			messageInfo.clear();
			bean.pageNum = 1;
			adapter.notifyDataSetChanged();

			launchRequest(WooRequestFactory.getJobsOpportunityDataRequest(bean));
			int updateNum = bundle.getInt("updateNum");
			int allnum = Integer.parseInt(Utils.messageInfo.userInfo
					.getCollectNum());

			Utils.messageInfo.userInfo.setCollectNum((allnum - updateNum) + "");

			Toast.makeText(this, "成功清除" + updateNum + "条", Toast.LENGTH_LONG)
					.show();
		}

	}

	@Override
	public void onRequestError(int exceptionType) {

	}

	public int getContentViewId() {
		return R.layout.listjobs_layout;

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.more:
			Intent intent = new Intent(this, DialogActivity.class);
			intent.putExtra("type", 1);
			startActivityForResult(intent, 0);
			break;
		case R.id.wancheng:
			more.setVisibility(View.VISIBLE);
			wancheng.setVisibility(View.GONE);
			if (adapter != null)
				adapter.hiddenButton();
			Utils.type = 2;
			launchRequest(WooRequestFactory.getApplyCollect());
			// adapter.notifyDataSetChanged();
			break;
		default:
			break;
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case 99:
			launchRequest(WooRequestFactory.getJobsOpportunityDataRequest(bean));
			break;

		default:
			break;
		}
		switch (resultCode) { // resultCode为回传的标记，我在B中回传的是RESULT_OK
		case 1:
			more.setVisibility(View.GONE);
			wancheng.setVisibility(View.VISIBLE);
			if (adapter != null)
				adapter.showButton();
			break;
		case 10:
			launchRequest(WooRequestFactory.xiaochu());
			break;
		case 99:
			mMessagesList.setPullLoadEnable(true);
			messageInfo.clear();
			bean.pageNum = 1;
			adapter.notifyDataSetChanged();
			launchRequest(WooRequestFactory.getJobsOpportunityDataRequest(bean));
			break;
		default:
			break;
		}
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		isRefresh = true;
		mMessagesList.setPullLoadEnable(true);
		messageInfo.clear();
		bean.pageNum = 1;
		if (adapter != null)
			adapter.notifyDataSetChanged();
		adapter = null;
		if (type == 1) {
			more.setVisibility(View.GONE);
		} else
			more.setVisibility(View.VISIBLE);
		wancheng.setVisibility(View.GONE);

		launchRequest(WooRequestFactory.getJobsOpportunityDataRequest(bean));
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		mHandler.postDelayed(new Runnable() {

			@Override
			public void run() {
				isRefresh = true;
				bean.pageNum += 1;
				launchRequest(WooRequestFactory
						.getJobsOpportunityDataRequest(bean));
			}

		}, 2000);

	}

	private String getTime() {
		return new SimpleDateFormat("MM-dd HH:mm", Locale.CHINA)
				.format(new Date());
	}
}
